package com.xyxeshang.es.music.backend.service.impl;

import com.alibaba.fastjson.JSONObject;

import com.xyxeshang.es.music.backend.entity.User;
import com.xyxeshang.es.music.backend.mapper.UserMapper;
import com.xyxeshang.es.music.backend.service.UserService;
import com.xyxeshang.es.music.backend.util.HttpClientUtil;
import com.xyxeshang.es.music.backend.util.JwtUtil;
import com.xyxeshang.es.music.backend.util.ResultUtil;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


/**
 * @author xyx-Eshang
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

	@Value("${APP_ID}")
	private String appId;

	@Value("${SECRET_KEY}")
	private String secretKey;

	@Resource
	UserMapper userMapper;

	/**
	 * 保存用户信息
	 *
	 * @param userOpenId
	 * @param userNickName
	 * @param userAvatarUrl
	 * @return
	 */
	@Override
	public ResultUtil<Null> save(String userOpenId, String userNickName, String userAvatarUrl) {
		try {
			//用户信息
			User user = new User()
					.setUserOpenId(userOpenId)
					.setUserNickName(userNickName)
					.setUserAvatarUrl(userAvatarUrl);

			//先查询是否有这个用户
			if (userMapper.getById(userOpenId) == null) {
				//不存在时存起来
				userMapper.save(user);
			} else {
				//存在的话更新信息
				userMapper.updateById(user);
			}
			return new ResultUtil<>();
		} catch (Exception e) {
			return new ResultUtil<>(e);
		}
	}

	/**
	 * 获取用户信息
	 *
	 * @param userOpenId
	 * @return
	 */
	@Override
	public ResultUtil<User> get(String userOpenId) {
		try {
			return new ResultUtil<>("user", userMapper.getById(userOpenId));
		} catch (Exception e) {
			return new ResultUtil<>(e);
		}
	}

	/**
	 * 用code换取token和openId
	 *
	 * @param code
	 * @return
	 */
	@Override
	public ResultUtil<Map<String, Object>> getOpenId(String code) {
		try {
			Map<String, Object> paramMap = new HashMap<>();
			paramMap.put("appid", this.appId);
			paramMap.put("secret", this.secretKey);
			paramMap.put("js_code", code);
			paramMap.put("grant_type", "authorization_code");
			paramMap.put("&connect_redirect", "1");
			String url = "https://api.weixin.qq.com/sns/jscode2session";

			//存有openid的response
			Map<String, Object> response = HttpClientUtil.getWithParams(url, paramMap);
			String resultJson = (String) response.get("body");
			JSONObject jsonObject = JSONObject.parseObject(resultJson);
			String userOpenId = jsonObject.getString("openid");

			//生成token
			String token = JwtUtil.createToken(new User().setUserOpenId(userOpenId));

			Map<String, Object> resultMap = new HashMap<>();
			resultMap.put("userOpenId", userOpenId);
			resultMap.put("token", token);

			return new ResultUtil<>("resultMap", resultMap);
		} catch (Exception e) {
			return new ResultUtil<>(e);
		}
	}
}
